import extend from 'dva-model-extend'
import settings from '@/settings'
import model from '@/baseModel'
import { folder, download, file, files } from '@/services/api'

const { pagination } = settings

export default extend(model, {
	namespace: 'file',
	state: {
		folder: ''
	},
	effects: {
		*reset(_, {
			put
		}) {
			yield put({
				type: 'updateState',
				payload: {
					data: undefined
				}
			})
		},
		*download({
			payload
		}, {
			call, put
		}) {
			yield call(download, payload)
		},
		*folder({
			payload
		}, {
			call,
			put
		}) {
			const { data: {
				name
			} } = yield call(folder, payload)
			yield put({
				type: 'updateState',
				payload: {
					folder: name
				}
			})
		},
		*fetchOne({
			payload
		}, {
			call,
			put
		}) {
			const { data } = yield call(file, payload)
			yield put({
				type: 'updateState',
				payload: {
					data
				}
			})
		},
		*fetchAll({
			payload = pagination
		}, {
			call,
			put
		}) {
			const { data } = yield call(files, payload)
			yield put({
				type: 'updateState',
				payload: {
					list: data,
					params: payload
				}
			})
		}
	}
})